﻿//---------------------------------------------------------------------------
// <copyright file="SplitButton.cs" company="Microsoft Corporation">
//     Copyright (C) Microsoft Corporation.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------

namespace Microsoft.Windows.Controls.Ribbon
{
    using System.Collections.ObjectModel;
    using System.Collections.Specialized;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    [TemplatePart(Name = "PART_ITEMSHOST", Type = typeof(ItemsControl))]
    public class SplitButton : Button
    {
        static SplitButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SplitButton), new FrameworkPropertyMetadata(typeof(SplitButton)));
        }

        #region Items property
        public Collection<object> Items
        {
            get
            {
                if (_items == null)
                {
                    _items = new ObservableCollection<object>();
                    _items.CollectionChanged += new NotifyCollectionChangedEventHandler(OnItemsCollectionChanged);
                }

                return _items;
            }
        }

        private ObservableCollection<object> _items;

        private void OnItemsCollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Reset:
                    if (_itemsHost != null)
                    {
                        _itemsHost.Items.Clear();
                    }

                    foreach (object item in Items)
                    {
                        if (_itemsHost != null)
                        {
                            _itemsHost.Items.Add(item);
                        }
                    }

                    break;
                case NotifyCollectionChangedAction.Add:
                    foreach (object item in e.NewItems)
                    {
                        if (_itemsHost != null)
                        {
                            _itemsHost.Items.Add(item);
                        }
                    }

                    break;
                case NotifyCollectionChangedAction.Remove:
                    foreach (object item in e.OldItems)
                    {
                        if (_itemsHost != null)
                        {
                            _itemsHost.Items.Remove(item);
                        }
                    }

                    break;
                case NotifyCollectionChangedAction.Replace:
                    if (e.NewItems.Count == 1 && e.OldItems.Count == 1)
                    {
                        if (_itemsHost != null)
                        {
                            int index = _itemsHost.Items.IndexOf(e.OldItems[0]);
                            if (index >= 0)
                            {
                                _itemsHost.Items[index] = e.NewItems[0];
                            }
                        }
                    }

                    break;
            }
        }

        #endregion

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (_itemsHost != null)
            {
                _itemsHost.Items.Clear();
            }

            _itemsHost = GetTemplateChild("PART_ITEMSHOST") as ItemsControl;

            // Add Tabs to the new panel
            if (_itemsHost != null)
            {
                _itemsHost.Items.Clear();
                foreach (object item in Items)
                {
                    _itemsHost.Items.Add(item);
                }
            }
        }

        private ItemsControl _itemsHost;
    }

    public class SplitButtonMenuItemStyleSelector : StyleSelector
    {
        public override Style SelectStyle(object item, DependencyObject container)
        {
            if (item is MenuItem || !(item is Visual))
            {
                return null;
            }

            // If we host visuals - create a simple style only with ContentPresenter
            Style style = new Style(typeof(MenuItem));
            ControlTemplate controlTemplate = new ControlTemplate(typeof(MenuItem));
            style.Setters.Add(new Setter(Control.TemplateProperty, controlTemplate));
            FrameworkElementFactory fef = new FrameworkElementFactory(typeof(ContentPresenter));
            fef.SetValue(ContentPresenter.ContentSourceProperty, "Header");
            controlTemplate.VisualTree = fef;
            style.Seal();
            return style;
        }
    }
}
